package chpt_06_02_strategy;

/**
 * @author Ping
 * @date 2023/5/11 8:41
 **/
public class TravelDemo {
    public static void main(String args[]) {
        PersonTravel personTravel = new PersonTravel() ;
        TransportStrategy byAir = new ByAirTransportStrategy() ;
        TransportStrategy byTrain = new ByTrainTransportStrategy() ;

        personTravel.setTransportStrategy(byAir);
        System.out.println("travel cost: " + personTravel.travelCost()) ;

        personTravel.setTransportStrategy(byTrain);
        System.out.println("travel cost: " + personTravel.travelCost()) ;





    }
}

interface TransportWay {
    public static final String BY_TRAIN = "bytrain" ;
    public static final String BY_AIR = "byair" ;
    public static final String BY_BYCYCLE = "bycycle" ;
}


class Person {
    /**
     * by train, by air, by bycycle
     */
   String transportWay ;

    /**
     * cost for travelling.
     * @return
     */
    public double travelCost() {
        double cost = 0.0 ;
        if (this.transportWay == TransportWay.BY_AIR) {
            return 2000.0;

        } else if (this.transportWay == TransportWay.BY_TRAIN) {
            return 500.0 ;
        } else if (this.transportWay == TransportWay.BY_BYCYCLE) {
            return 50.0 ;
        }
        return 0.0 ;

    }


}

/**
 * Strategy role
 */
interface TransportStrategy {
    public double getCost() ;
}


/**
 * context role
 */
class PersonTravel {
    private TransportStrategy transportStrategy ;
    public void setTransportStrategy(TransportStrategy transportStrategy) {
        this.transportStrategy = transportStrategy;
    }
    public double travelCost() {
        return this.transportStrategy.getCost() ;
    }
}

/**
 * concrete strategy role:
 */
class ByAirTransportStrategy implements TransportStrategy {
    @Override
    public double getCost() {
        System.out.println("[INFO] travelling by air.") ;
        return 1000.0;
    }
}

class ByTrainTransportStrategy implements TransportStrategy {
    @Override
    public double getCost() {
        System.out.println("[INFO] travelling by tran.") ;
        return 500.0;
    }
}
class BycycleTransportStrategy implements TransportStrategy {
    @Override
    public double getCost() {
        System.out.println("[INFO] travelling by bycycle.") ;
        return 50.0;
    }
}



